#include "Skill/SkillTuXi.h"

#include "base/core/log.h"
#include "base/core/com_define.h"
#include "Character/MovePart.h"
#include "Character/SkillPart.h"
#include "base/core/utility.h"
#include "Scene/MapMgr.h"
SkillTuXi::SkillTuXi()
{

}
SkillTuXi::~SkillTuXi()
{

}

void SkillTuXi::refreshData()
{
	SkillBase::refreshData();
	m_listFindTarget.clear();
}

void SkillTuXi::OnDamageTimedoPlayer(uint32_t skillId, cList & vtargetList)
{
	cList::iterator iter;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);
	m_pPart->KillTimedo(m_nGlobelID);
	//将每个目标移到目标点上
	iter = vtargetList.begin();
	for (; iter != vtargetList.end(); ++iter)
	{
		Creature * pCreature = m_pPart->GetCreature(*iter);
		if (pCreature)
		{
			if (pCreature->GetCid() == m_pPart->GetMasterCid())
			{
				continue;
			}
			MovePart * pPart = dynamic_cast<MovePart*>(pCreature->GetPart(PART_MOVE));
			if (pPart)
			{
				pPart->Teleporting(m_targetPos);
				LogDebugFmtPrint("tuxi cid=%llu,pos(%f,%f,%f)", m_targetPos.x, m_targetPos.y, m_targetPos.z);
			}
		}
	}
	
	//计算技能结果
	m_pPart->SkillResult(pSkill->_Id, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, vtargetList);

	if (pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		//m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{
		m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
}
void SkillTuXi::OnDamageTimedo(uint32_t skillId)
{
	cList::iterator iter;
	CharIDType targetCid = 0;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);

	m_pPart->KillTimedo(m_nGlobelID);



	//根据时间点来取找当前目标
	if (Range_none_target > pSkill->_pSkillmeta->rangeType)
	{
		MMOLOG_PROCESS_ERROR(m_curDamageCount < m_vTargetCids.size());
		targetCid = m_vTargetCids[m_curDamageCount];
	}

	//LogDebugFmtPrint("skill damagetimeDo, curdamgeCount:%d, skillId:%d, targetCid:%d", pSkill->curDamageCount, skillId, targetCid);
	if (m_curDamageCount==0)
	{
		m_pPart->FindCreatures(pSkill->_pSkillmeta,m_masterPos, m_skillDir, targetCid, m_targetPos, m_curDamageCount, m_focoTime, m_listFindTarget);


		std::vector<int32_t> vDistance;
		CommonApi::SplitStrToVecInt(pSkill->_pSkillmeta->displaceDistance, ",", &vDistance);
		if (vDistance.size()<1)
		{
			LogDebugFmtPrint("skillid=%d param is error pSkill->_pSkillmeta->displaceDistance", pSkill->_pSkillmeta->skillID);
			return;
		}

		Point3<float> targetPos = m_pPart->GetMasterPos() + m_pPart->GetMasterDir()*vDistance[0] / 1000.0f;
		float dict = vDistance[0] / 1000.0f;
		Point3<float> srcPos = m_pPart->GetMasterPos();
		Map* pMap = g_GetMapMgr()->GetMap(m_pPart->GetMaster()->GetMapId());
		bool ret = false;
		if (pMap)
		{
			for (float d = dict; d >= 0.1f; d -= 0.1f)
			{
				Point3<float> tmpPos = srcPos + m_pPart->GetMasterDir() * d;
				targetPos = srcPos;
				ret = pMap->FindNearestPos(tmpPos.x, tmpPos.z, tmpPos.y, &targetPos.x, &targetPos.z, &targetPos.y, nullptr);
				if (ret)
				{
					break;
				}
			}
		}

		//将每个目标移到目标点上
		m_listFindTarget.push_back(m_pPart->GetMasterCid());
		iter = m_listFindTarget.begin();
		for (; iter != m_listFindTarget.end();++iter)
		{
			Creature * pCreature = m_pPart->GetCreature(*iter);
			if (pCreature)
			{
				MovePart * pPart = dynamic_cast<MovePart*>(pCreature->GetPart(PART_MOVE));
				if (pPart)
				{
					pPart->Teleporting(targetPos);
					//LogDebugFmtPrint("tuxi cid=%llu,pos(%f,%f,%f)",targetPos.x,targetPos.y,targetPos.z);
				}
			}
		}
	}
	
	//计算技能结果
	m_pPart->SkillResult(pSkill->_Id, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, m_listFindTarget);

	if (pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{
		{
			//技能一次释放完成，需要删通知删除
			
			m_pPart->RemoveSkillFightInfo(m_nGlobelID);
		}
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
}
